package com.vip.jwt.common.utils.tree;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 构建树封装
 *
 * @author vip
 * @date 2019-06-14 9:18
 */
public class VipTreeUtil {

    private List<VipTree> treeList;
    private Integer layer;

    public VipTreeUtil(List<VipTree> treeList) {
        this.treeList = treeList;
    }

    /**
     * 正常将列表递归成树（无限制）
     * @return
     */
    public List<VipTree> buildTree() {
        List<VipTree> root = new ArrayList<>();
        for (int i = 0; i < treeList.size(); i++) {
            VipTree o = treeList.get(i);
            if (StrUtil.isEmpty(o.getParentId())) {
                VipTree vo = createBranch(treeList, o);
                root.add(vo);
            }
        }
        return root;
    }

    /**
     * 显示到几层
     * 共12345
     * 传入3
     * 显示123
     * @param endLayer
     * @return
     */
    public List<VipTree> buildTreeEnd(Integer endLayer) {
        this.layer = endLayer;
        List<VipTree> root = new ArrayList<>();
        for (int i = 0; i < treeList.size(); i++) {
            VipTree o = treeList.get(i);
            if (StrUtil.isEmpty(o.getParentId())) {
                VipTree vo = createBranch2(treeList, o);
                root.add(vo);
            }
        }
        return root;
    }

    /**
     * 从第几层开始显示
     * 共12345
     * 传入3
     * 显示345
     * @param startLayer
     * @return
     */
    public List<VipTree> buildTreeStart(Integer startLayer) {
        List<VipTree> root = new ArrayList<>();
        for (int i = 0; i < treeList.size(); i++) {
            VipTree o = treeList.get(i);
            if (o.getLayer().equals(startLayer)) {
                VipTree vo = createBranch(treeList, o);
                root.add(vo);
            }
        }
        return root;
    }

    private VipTree createBranch(List<VipTree> list, VipTree o) {
        List<VipTree> node = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            VipTree newNode = list.get(i);
            if (StrUtil.isNotEmpty(newNode.getParentId()) && newNode.getParentId().equals(o.getId())) {
                VipTree vo = createBranch(list, newNode);
                node.add(vo);
            }
        }
        if (!node.isEmpty()) {
            o.setChildren(node);
        }else{
            o.setChildren(new ArrayList<>());
        }
        return o;
    }

    private VipTree createBranch2(List<VipTree> list, VipTree o) {
        List<VipTree> node = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            VipTree newNode = list.get(i);
            if (StrUtil.isNotEmpty(newNode.getParentId()) && newNode.getParentId().equals(o.getId())) {
                VipTree vo = createBranch2(list, newNode);
                node.add(vo);
            }
        }
        if (!node.isEmpty()) {
            if(o.getLayer()<this.layer){
                o.setChildren(node);
            }else{
                o.setChildren(new ArrayList<>());
            }
        }else{
            o.setChildren(new ArrayList<>());
        }
        return o;
    }

}
